package ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import model.TibNeeg;
import model.YawmSaub;

public class UIPupMenu extends JPopupMenu{
	
	/**
	 * Default UID
	 */
	private static final long serialVersionUID = 1L;
	private JPanel pan;
	private TibNeeg kuv_tn;
    private YawmSaub yawmSaub;
    
	public UIPupMenu(JPanel p, TibNeeg tn, YawmSaub yS) {
		this.pan = p;
		this.kuv_tn = tn;
		this.yawmSaub = yS;
		
		this.setPopupSize(100, 50);
		
		// New project menu
        JMenu menuHloov = new JMenu("Hloov...");
        menuHloov.setMnemonic(KeyEvent.VK_P);
        menuHloov.getAccessibleContext().setAccessibleDescription(
                "Hloov...");
        
        JMenuItem hlooovNpeItem = new JMenuItem("Hloov Npe");
        hlooovNpeItem.addActionListener(new ActionListener() {
        	 
            public void actionPerformed(ActionEvent e) {
                
            	ImageIcon icon = createImageIcon("images/middle.gif");
            	String s = (String)JOptionPane.showInputDialog(
                        pan,
                        "Npe : "+ kuv_tn.getNpe() +" \nPauv tshiab hu ua : ",
                        "Hloov Npe Tshiab",
                        JOptionPane.PLAIN_MESSAGE,
                        icon,
                        null,
                        kuv_tn.getNpe());
            	
            	if(s != null && s.length()>0){
            		//System.out.println("--> "+ s);
            		kuv_tn.setNpe(s);
            		//getInstance for refresh family tree
            		//Control_UI clt = Control_UI.getInstance();
            		//clt.reFreshTree();
            		pan.revalidate();
            		pan.repaint();
            	}
            }
        });//END Hloov npe action
        
        menuHloov.add(hlooovNpeItem);
        
        JMenuItem hlooovXeemItem = new JMenuItem("Hloov Xeem");
        hlooovXeemItem.addActionListener(new ActionListener() {
       	 
            public void actionPerformed(ActionEvent e) {
            	ImageIcon icon = createImageIcon("images/middle.gif");
            	String s = (String)JOptionPane.showInputDialog(
                        pan,
                        "Xeem : "+ kuv_tn.getXeem() +" \nPauv tshiab ua : ",
                        "Hloov Xeem",
                        JOptionPane.PLAIN_MESSAGE,
                        icon,
                        null,
                        kuv_tn.getXeem());
            	
            	if(s != null && s.length()>0){
            		//System.out.println("--> "+ s);
            		kuv_tn.setXeem(s);
            		pan.revalidate();
            		pan.repaint();
            	}
            }            
        });//END Hloov xeem action
        menuHloov.add(hlooovXeemItem);
        
        
        
        // ++++++ Menu Hloov ++++++
        
        JMenu menuNtxiv = new JMenu("Ntxiv...");
        menuNtxiv.setMnemonic(KeyEvent.VK_F);
        
        // Item ntxiv txiv
        JMenuItem ntxivTxivItem = new JMenuItem("Ntxiv Txiv");
        ntxivTxivItem.addActionListener(new ActionListener() {
       	 
            public void actionPerformed(ActionEvent e) {
            	if(kuv_tn.getTxiv() == null){
            		new DialogNtxivTxiv(kuv_tn, yawmSaub);
            		
            	}else{
            		// Show message Txiv exist
    		    	JOptionPane.showMessageDialog(pan, kuv_tn.getNpe()+" muaj txiv lawm!", 
    		    			"Ntxiv info", JOptionPane.INFORMATION_MESSAGE );
            	}
            }
        });
        menuNtxiv.add(ntxivTxivItem);
      
     // Item ntxiv txiv
        JMenuItem ntxivMeNyuamItem = new JMenuItem("Ntxiv Me Nyuam");
        ntxivMeNyuamItem.addActionListener(new ActionListener() {
       	 
            public void actionPerformed(ActionEvent e) {
            	
            	new DialogNtxivMeNyuam(kuv_tn, yawmSaub);
            	
            }
        });
        menuNtxiv.add(ntxivMeNyuamItem);
        
        
        // ++++++ Menu Delete ++++++
        
        JMenuItem menuDelete = new JMenuItem("Delete");
        menuDelete.addActionListener(new ActionListener() {
       	 
            public void actionPerformed(ActionEvent e) {
            	yawmSaub.deleteTibNeeg(kuv_tn);
            	yawmSaub.refreshTree();
            }
        });
        
        
        
        // Add to popup menu
        add(menuHloov);
        add(menuNtxiv);
        add(menuDelete);
        
	}
	
	/** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        
    	return null;
    }
    
	
	
}
